﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Implement;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Holiday;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Holiday
{
    public class SpHolidayCommandHarder : IRequestHandler<SpHolidayCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<WF_Recode> repository;
        private readonly IBaseRepository<Holidays> baseRepository;

        public  SpHolidayCommandHarder(IMapper mapper, IBaseRepository<WF_Recode> repository, IBaseRepository<Holidays> baseRepository)
        {
            this.mapper = mapper;
            this.repository = repository;
            this.baseRepository = baseRepository;
        }

        public  Task<APIResult<object>> Handle(SpHolidayCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var kk = baseRepository.GetAll().Result.Where(x => x.HoId == request.HoId).FirstOrDefault();
            var xi = mapper.Map<WF_Recode>(request);
      
      

            repository.Add(xi);

            if (xi.zt == 2 && xi.approvalId == 1)
            {

                kk.Spzt = Approval.已拒绝.ToString();
                kk.yj = xi.YiJian;
                 baseRepository.Update(kk);
            }
            if (xi.zt == 3 && xi.approvalId == 1)
            {

                kk.Spzt = Approval.处理中.ToString();
                 baseRepository.Update(kk);
            }
            if (xi.zt == 2 && xi.approvalId == 2)
            {

                kk.Spzt = Approval.已拒绝.ToString();
                kk.yj = xi.YiJian;
                baseRepository.Update(kk);
            }
            if (xi.zt == 3 && xi.approvalId == 2)
            {

                kk.Spzt = Approval.已完成.ToString();
                 baseRepository.Update(kk);
            }

      
            result.Code = APIEnum.审批成功;
            result.Msg = APIEnum.审批成功.ToString();


            return  Task.FromResult(result);
        }
    }
}

